import os # 载入os库
from docx import Document

d = 'D:\\python_book_resource' 
keyword = '通行密钥' # 设定要搜索的关键字
print('搜索开始') # 提示用户搜索开始
for root, dirs, files in os.walk(d): # 获取指定文件夹内所有文件的简化写法
    for file in files:
       ext = os.path.splitext(file)[1][1:]
       if ext.lower() in ['txt', 'log', 'csv']: #其他可能存在的可用记事本访问的格式也可以加入进来
        	path = os.path.join(root, file)
       	 	with open(path, 'r') as fr:
            	if keyword in fr.read():
                	print('找到相关文件，位于： {0}'.format(path))
       else:
        	if ext.lower() == 'docx':
           		path = os.path.join(root, file)
            	document = Document(path)
             	word_content = ''
                for p in document.paragraphs: # 获取每一个段落的字符串
					word_content = word_content + p.runs[0].text + '\r'
                if keyword in word_content:
                	print(word_content)    